package org.flexthinker.binding.bean.support
{
    import org.flexthinker.binding.bean.AutoBinding;
    import org.flexthinker.binding.bean.BeanProperty;
    import org.flexthinker.binding.bean.Binding;
    import org.flexthinker.binding.bean.BindingListener;
    import org.flexthinker.binding.bean.BindingListenerCounter;
    import org.flexthinker.binding.bean.UpdateStrategy;
    import org.flexthinker.binding.conversion.support.PropertyConverter;
    import org.flexthinker.binding.value.support.MockBean;
    import org.hamcrest.assertThat;
    import org.hamcrest.number.lessThan;
    import org.hamcrest.object.equalTo;

    public class AutoBindingPerformanceTest
    {
        public function AutoBindingPerformanceTest()
        {
        }

        [Ignore(message="Enable this only when you want to test performance")]
        [Test]
        public function testPerformance():void
        {
            var sourceBean:MockBean = new MockBean();
            var targetBean:MockBean = new MockBean();
            var binding:Binding = new AutoBinding( UpdateStrategy.READ_WRITE,
                                      sourceBean, BeanProperty.create("simpleString"),
                                        targetBean, BeanProperty.create("simpleInt") );
            var listener:BindingListenerCounter = new BindingListenerCounter();
            var repetCount:int = 100000;

            binding.addBindingListener(listener);
            binding.setConverter( PropertyConverter.STRING_TO_INT_CONVERTER );

            binding.bind();


            var startTime:uint = new Date().getUTCMilliseconds();

            for (var i:int = 0; i < repetCount; i++)
            {
                if ( i % 2 == 0 )
                {
                    sourceBean.simpleString = "123";
                }
                else
                {
                    targetBean.simpleInt = 456;
                }
            }

            // at consecutive runs, the time should be around 15 ms, but at first run it usualy takes about 300-600ms
            assertThat("WARNING ! AutoBinding Performance may not be good anymore",
                    (new Date().getUTCMilliseconds() - startTime), lessThan(250));

            assertThat(listener.counter, equalTo(repetCount*4 + 2));
            assertThat(listener.sourceChangedCounter, equalTo(repetCount));
            assertThat(listener.targetChangedCounter, equalTo(repetCount));

        }

    }
}
